'use strict';
exports.main = async (event, context) => {
	const db = uniCloud.databaseForJQL({
			event,
			context 
		});
		db.setUser({
			role: ['admin']
		})
		let iteminfo = await db.collection("item").where({
			_id: event.queryStringParameters.id
		}).get();
		if(iteminfo.data.length==0){
			return {msg:"id错误"}
		}
	let userinfo = await db.collection("uni-id-users").where({
		_id: iteminfo.data[0].user_id
	}).get();
	if(userinfo.data[0].vip==0||new Date().getTime()>userinfo.data[0].vip){
		const len=await db.collection("x2ad").count()
		var random=Math.round(Math.random()*(len.total-1))
		const res=await db.collection("x2ad").skip(random).limit(1).get()
		await db.collection("x2ad").where({_id:res.data[0]._id}).update({browse:++res.data[0].browse})
		return {msg:"获取成功",data:res.data[0]}
	}else{
		return {msg:"会员免广告"}
	}
};
